﻿using Newtonsoft.Json.Linq;
using PhalApiClientSDK;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XiTi
{
    public class ApiHelper
    {
        private static PhalApiClient createClient()
        {
            return PhalApiClient.create().withHost("http://www.sfzd5.com/noipapi/phalapi/public/");
        }

        /**
         * {
    "ret": 200,
    "data": {
        "items": [],
        "total": 0
    },
    "msg": ""
}
         */
        public static JObject getList(string km, out string msg)
        {
            msg = "";
            PhalApiClientResponse response = createClient().withService("App.Mryt_Mryt.GetList")
                .withParams("km", km)
                .request();
            if (response.isSuccess() && response.data != null)
            {
                return response.data;
            }
            msg = response.msg;
            return null;
        }

        public static DataItem getHtml(int id, out string msg)
        {
            msg = "";
            PhalApiClientResponse response = createClient().withService("App.Mryt_Mryt.Get")
                .withParams("id", id.ToString())
                .request();
            if (response.isSuccess() && response.data != null && response.data.ContainsKey("sucess") && response.data.ContainsKey("data"))
            {
                if ((bool)response.data["sucess"])
                {
                    if (response.data["data"].ToString().Length > 10)
                        return response.data["data"].ToObject<DataItem>();
                }
            }
            msg = response.msg;
            return null;
        }

        //question_title
        public static int indset(DataItem datItem, out string msg)
        {
            msg = "";
            PhalApiClientResponse response = createClient().withService("App.Mryt_Mryt.Insert")
                .withParams("title", datItem.title)
                .withParams("km", datItem.km)
                .withParams("html", datItem.html)
                .withParams("fullhtml", datItem.fullhtml)
                .request();
            if (response.isSuccess() && response.data != null && response.data.ContainsKey("id"))
            {
                if ((int)response.data["id"] > 0)
                {
                    return (int)response.data["id"];
                }
            }
            msg = response.msg;
            return -1;
        }
    }
}
